﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using Model;
using System.Collections.Generic;//Please add references
namespace Maticsoft.DAL
{
	/// <summary>
	/// 数据访问类:Ticket
	/// </summary>
	public partial class TicketDAO
	{
		public TicketDAO()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("ID", "Ticket"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Ticket");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Ticket model)
		{
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Ticket(");
            strSql.Append("Name,BegDate,EndDate,TotalNum,UsableNum,Crowd,Pic,IsShow,Type,Discount,OldPrice,NewPrice,Describe,HavNum,Remark)");
            strSql.Append(" values (");
            strSql.Append("@Name,@BegDate,@EndDate,@TotalNum,@UsableNum,@Crowd,@Pic,@IsShow,@Type,@Discount,@OldPrice,@NewPrice,@Describe,@HavNum,@Remark)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.VarChar,150),
					new SqlParameter("@BegDate", SqlDbType.Date,3),
					new SqlParameter("@EndDate", SqlDbType.Date,3),
					new SqlParameter("@TotalNum", SqlDbType.Int,4),
					new SqlParameter("@UsableNum", SqlDbType.Int,4),
					new SqlParameter("@Crowd", SqlDbType.VarChar,50),
					new SqlParameter("@Pic", SqlDbType.VarChar,500),
					new SqlParameter("@IsShow", SqlDbType.Bit,1),
					new SqlParameter("@Type", SqlDbType.VarChar,50),
					new SqlParameter("@Discount", SqlDbType.VarChar,50),
					new SqlParameter("@OldPrice", SqlDbType.Decimal,9),
					new SqlParameter("@NewPrice", SqlDbType.Decimal,9),
					new SqlParameter("@Describe", SqlDbType.Text),
					new SqlParameter("@HavNum", SqlDbType.Int,4),
					new SqlParameter("@Remark", SqlDbType.VarChar,350)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.BegDate;
            parameters[2].Value = model.EndDate;
            parameters[3].Value = model.TotalNum;
            parameters[4].Value = model.UsableNum;
            parameters[5].Value = model.Crowd;
            parameters[6].Value = model.Pic;
            parameters[7].Value = model.IsShow;
            parameters[8].Value = model.Type;
            parameters[9].Value = model.Discount;
            parameters[10].Value = model.OldPrice;
            parameters[11].Value = model.NewPrice;
            parameters[12].Value = model.Describe;
            parameters[13].Value = model.HavNum;
            parameters[14].Value = model.Remark;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Ticket model)
		{
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Ticket set ");
            strSql.Append("Name=@Name,");
            strSql.Append("BegDate=@BegDate,");
            strSql.Append("EndDate=@EndDate,");
            strSql.Append("TotalNum=@TotalNum,");
            strSql.Append("UsableNum=@UsableNum,");
            strSql.Append("Crowd=@Crowd,");
            strSql.Append("Pic=@Pic,");
            strSql.Append("IsShow=@IsShow,");
            strSql.Append("Type=@Type,");
            strSql.Append("Discount=@Discount,");
            strSql.Append("OldPrice=@OldPrice,");
            strSql.Append("NewPrice=@NewPrice,");
            strSql.Append("Describe=@Describe,");
            strSql.Append("HavNum=@HavNum,");
            strSql.Append("Remark=@Remark");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.VarChar,150),
					new SqlParameter("@BegDate", SqlDbType.Date,3),
					new SqlParameter("@EndDate", SqlDbType.Date,3),
					new SqlParameter("@TotalNum", SqlDbType.Int,4),
					new SqlParameter("@UsableNum", SqlDbType.Int,4),
					new SqlParameter("@Crowd", SqlDbType.VarChar,50),
					new SqlParameter("@Pic", SqlDbType.VarChar,500),
					new SqlParameter("@IsShow", SqlDbType.Bit,1),
					new SqlParameter("@Type", SqlDbType.VarChar,50),
					new SqlParameter("@Discount", SqlDbType.VarChar,50),
					new SqlParameter("@OldPrice", SqlDbType.Decimal,9),
					new SqlParameter("@NewPrice", SqlDbType.Decimal,9),
					new SqlParameter("@Describe", SqlDbType.Text),
					new SqlParameter("@HavNum", SqlDbType.Int,4),
					new SqlParameter("@Remark", SqlDbType.VarChar,350),
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.BegDate;
            parameters[2].Value = model.EndDate;
            parameters[3].Value = model.TotalNum;
            parameters[4].Value = model.UsableNum;
            parameters[5].Value = model.Crowd;
            parameters[6].Value = model.Pic;
            parameters[7].Value = model.IsShow;
            parameters[8].Value = model.Type;
            parameters[9].Value = model.Discount;
            parameters[10].Value = model.OldPrice;
            parameters[11].Value = model.NewPrice;
            parameters[12].Value = model.Describe;
            parameters[13].Value = model.HavNum;
            parameters[14].Value = model.Remark;
            parameters[15].Value = model.ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Ticket ");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Ticket ");
			strSql.Append(" where ID in ("+IDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Ticket GetModel(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 * from Ticket ");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			Ticket model=new Ticket();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Ticket DataRowToModel(DataRow row)
		{
			Ticket model=new Ticket();
			if (row != null)
			{
				if(row["ID"]!=null && row["ID"].ToString()!="")
				{
					model.ID=int.Parse(row["ID"].ToString());
				}
				if(row["Name"]!=null)
				{
					model.Name=row["Name"].ToString();
				}
				if(row["BegDate"]!=null && row["BegDate"].ToString()!="")
				{
					model.BegDate=DateTime.Parse(row["BegDate"].ToString());
				}
				if(row["EndDate"]!=null && row["EndDate"].ToString()!="")
				{
					model.EndDate=DateTime.Parse(row["EndDate"].ToString());
				}
				if(row["TotalNum"]!=null && row["TotalNum"].ToString()!="")
				{
					model.TotalNum=int.Parse(row["TotalNum"].ToString());
				}
				if(row["UsableNum"]!=null && row["UsableNum"].ToString()!="")
				{
					model.UsableNum=int.Parse(row["UsableNum"].ToString());
				}
				if(row["Crowd"]!=null)
				{
					model.Crowd=row["Crowd"].ToString();
				}
				if(row["Pic"]!=null)
				{
					model.Pic=row["Pic"].ToString();
				}
				if(row["IsShow"]!=null && row["IsShow"].ToString()!="")
				{
					if((row["IsShow"].ToString()=="1")||(row["IsShow"].ToString().ToLower()=="true"))
					{
						model.IsShow=true;
					}
					else
					{
						model.IsShow=false;
					}
				}
				if(row["Type"]!=null)
				{
					model.Type=row["Type"].ToString();
				}
                if (row["Discount"] != null)
                {
                    model.Discount = row["Discount"].ToString();
                }
                if (row["OldPrice"] != null && row["OldPrice"].ToString() != "")
                {
                    model.OldPrice = decimal.Parse(row["OldPrice"].ToString());
                }
                if (row["NewPrice"] != null && row["NewPrice"].ToString() != "")
                {
                    model.NewPrice = decimal.Parse(row["NewPrice"].ToString());
                }
                if (row["Describe"] != null)
                {
                    model.Describe = row["Describe"].ToString();
                }
                if (row["HavNum"] != null && row["HavNum"].ToString() != "")
                {
                    model.HavNum = int.Parse(row["HavNum"].ToString());
                }
                if (row["Remark"] != null)
                {
                    model.Remark = row["Remark"].ToString();
                }
                if (row.Table.Columns.Contains("usetime"))
                {
                    if (DateTime.Parse(row["usetime"].ToString()) == DateTime.Parse("1900-01-01"))
                        model.IsSY = 0;
                    else
                        model.IsSY = 1;
                }
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM Ticket ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" ID,Name,BegDate,EndDate,TotalNum,UsableNum,Crowd,Pic,IsShow,Type ");
			strSql.Append(" FROM Ticket ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Ticket ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.ID desc");
			}
			strSql.Append(")AS Row, T.*  from Ticket T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

        /// <summary>
        /// 获取我的券
        /// </summary>
        /// <param name="Fromusername"></param>
        /// <returns></returns>
        public List<Ticket> GetMyTicket(string Fromusername)
        {
            string sql = string.Format(@"select t.*,p.usetime from TicketForPerson p left JOIN Ticket t on p.ticketid=t.id
WHERE p.FromUserName='{0}' ORDER by p.UseTime", Fromusername);
            List<Ticket> list = new List<Ticket>();
            DataSet ds = DbHelperSQL.Query(sql);
            Ticket t = new Ticket();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                t = DataRowToModel(dr);
                list.Add(t);
            }
            return list;
        }

        public List<Ticket> GetMyTicketIndex(string Fromusername)
        {
            string sql = string.Format(@"select t.*,p.usetime from TicketForPerson p left JOIN Ticket t on p.ticketid=t.id
WHERE p.FromUserName='{0}' and UseTime='1900-01-01 00:00:00.000' ORDER by p.UseTime", Fromusername);
            List<Ticket> list = new List<Ticket>();
            DataSet ds = DbHelperSQL.Query(sql);
            Ticket t = new Ticket();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                t = DataRowToModel(dr);
                list.Add(t);
            }
            return list;
        }
		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Ticket";
			parameters[1].Value = "ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

